package Frame.screen;

import FCRendering.FCRConstants;
import FCRendering.FCRMaster;
import FCRendering.ImageUtil;
import FCRendering.sw.FCRLoadData;
import utils.DateUtil;
import utils.FCUIConfig;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Set;

import static FCRendering.FCRMaster.*;

public class DefaultSingleReadyScreen extends IScreen {
    //是否作为主机启动
    private boolean asServer = false;

    public DefaultSingleReadyScreen(){
        super();
    }
    public DefaultSingleReadyScreen(boolean isServer){
        super();
        asServer = isServer;
    }
    private Font font1 = new Font(FCRConstants.FONT_BLACK, 0, (int)(20* fcv.getProportion()));
    private Font font2 = new Font(FCRConstants.FONT_BLACK, 0, (int)(20* fcv.getProportion()));
    @Override
    public void dispatchPaintComponent(Graphics g, FCRMaster fcrMaster) {
        g.setColor(new Color(163, 163, 163));
        g.drawImage(ImageUtil.getImage("getready"), 0, 0, fcrMaster.getWidth(), fcrMaster.getHeight(), this);
        if (fcv.getEnterBu() == 11)
            g.fillRect((int) (906 * fcv.getProportion()), (int) (940 * fcv.getProportion()), (int) (14 * fcv.getProportion()),
                    (int) (32 * fcv.getProportion()));
        if (fcv.getEnterBu() == 12)
            g.fillRect((int) (1060 * fcv.getProportion()), (int) (940 * fcv.getProportion()), (int) (14 * fcv.getProportion()),
                    (int) (32 * fcv.getProportion()));
        if (fcv.getEnterBu() == 13)
            g.fillRect((int) (1214 * fcv.getProportion()), (int) (940 * fcv.getProportion()), (int) (14 * fcv.getProportion()),
                    (int) (32 * fcv.getProportion()));
        // 存档信息
        ArrayList<String> sa = saveList.getSaveTimelist();
        int i = 0;
        for (String ti : sa) {
            g.setColor(new Color(163, 163, 163));
            g.drawRect((int) (558.5 * fcv.getProportion()), (int) ((224 + 43 * i) * fcv.getProportion()),
                    (int) (803 * fcv.getProportion()), (int) (39 * fcv.getProportion()));
            g.setFont(font1);
            g.drawString(ti, (int) (1200 * fcv.getProportion()), (int) ((246 + 43 * i) * fcv.getProportion()));
            g.setFont(font2);
            g.setColor(Color.white);
            g.drawString(saveList.getSaveInfoOfTime(ti), (int) (568.5 * fcv.getProportion()),
                    (int) ((246 + 43 * i) * fcv.getProportion()));
            g.setColor(Color.red);
            if (i == fcv.getSaveMouseEnter())
                g.drawRect((int) (560.5 * fcv.getProportion()), (int) ((226 + 43 * i) * fcv.getProportion()),
                        (int) (799 * fcv.getProportion()), (int) (35 * fcv.getProportion()));
            if (i == fcv.getSaveMouseClick())
                g.drawRect((int) (559.5 * fcv.getProportion()), (int) ((225 + 43 * i) * fcv.getProportion()),
                        (int) (801 * fcv.getProportion()), (int) (37 * fcv.getProportion()));
            if (++i > sa.size() || i > 15)
                break;
        }
        // 新建存档窗口
        if (fcv.getClickBu() == 12) {
            g.drawImage(ImageUtil.getImage("newsave"), (int) (793 * fcv.getProportion()), (int) (439 * fcv.getProportion()),
                    (int) (395 * fcv.getProportion()), (int) (262 * fcv.getProportion()), this);
            g.drawImage(charLib.getCharacterOfName("player1").getActionImage(0, 0, false), (int) (808 * fcv.getProportion()),
                    (int) (454 * fcv.getProportion()), (int) (108 * fcv.getProportion()), (int) (108 * fcv.getProportion()), this);
            g.drawImage(charLib.getCharacterOfName("player2").getActionImage(0, 0, false), (int) (936 * fcv.getProportion()),
                    (int) (454 * fcv.getProportion()), (int) (108 * fcv.getProportion()), (int) (108 * fcv.getProportion()), this);
            g.drawImage(charLib.getCharacterOfName("player3").getActionImage(0, 0, false), (int) (1062 * fcv.getProportion()),
                    (int) (454 * fcv.getProportion()), (int) (108 * fcv.getProportion()), (int) (108 * fcv.getProportion()), this);
            g.setColor(Color.red);
            if (fcv.getSelectPlayer() == 0)
                g.drawRect((int) (807 * fcv.getProportion()), (int) (453 * fcv.getProportion()), (int) (110 * fcv.getProportion()),
                        (int) (110 * fcv.getProportion()));
            if (fcv.getSelectPlayer() == 1)
                g.drawRect((int) (935 * fcv.getProportion()), (int) (453 * fcv.getProportion()), (int) (110 * fcv.getProportion()),
                        (int) (110 * fcv.getProportion()));
            if (fcv.getSelectPlayer() == 2)
                g.drawRect((int) (1061 * fcv.getProportion()), (int) (453 * fcv.getProportion()), (int) (110 * fcv.getProportion()),
                        (int) (110 * fcv.getProportion()));
            g.setColor(new Color(163, 163, 163));
            if (fcv.getEnterBu() == 14)
                g.fillRect((int) (870 * fcv.getProportion()), (int) (646 * fcv.getProportion()), (int) (14 * fcv.getProportion()),
                        (int) (32 * fcv.getProportion()));
            if (fcv.getEnterBu() == 15)
                g.fillRect((int) (1024 * fcv.getProportion()), (int) (646 * fcv.getProportion()), (int) (14 * fcv.getProportion()),
                        (int) (32 * fcv.getProportion()));
        }
        if(asServer) {
            g.setFont(fcrMaster.getFont());
            g.setColor(Color.white);
            String tips = "服务器模式：当前你的服务端口是5500";
            g.drawString(tips, (int) (50 * fcv.getProportion()), (int) (50 * fcv.getProportion()));
        }
    }

    @Override
    public void setBounds() {
        font1 = new Font(FCRConstants.FONT_BLACK, 0, (int)(20* fcv.getProportion()));
        font2 = new Font(FCRConstants.FONT_BLACK, 0, (int)(20* fcv.getProportion()));
    }

    @Override
    public void listen_mouse_move_game(MouseEvent g) {

        int ti = FCRMaster.fcv.getFrames();
        int clickBu = FCRMaster.fcv.getClickBu();
        int x = g.getX(), y = g.getY();
        if (ti > 960) {// 单人游戏准备界面
            if (clickBu == 1) {
                if (FCUIConfig.getIn().checkUIClick("singlePlayBack", x, y)) {
                    FCRMaster.fcv.setEnterBu(11);// 返回
                } else if (FCUIConfig.getIn().checkUIClick("singlePlayNew", x, y)) {
                    FCRMaster.fcv.setEnterBu(12);// 新建
                } else if (FCUIConfig.getIn().checkUIClick("singlePlayStart", x, y)) {
                    FCRMaster.fcv.setEnterBu(13);// 开始
                } else if(FCUIConfig.getIn().checkUIClick("singlePlayArea", x, y)) {
                    FCRMaster.fcv.setSaveMouseEnter((int) ((y - 224 * FCRMaster.fcv.getProportion()) / (43* FCRMaster.fcv.getProportion())));
                } else{
                    FCRMaster.fcv.setSaveMouseEnter(-1);
                    FCRMaster.fcv.setEnterBu(0);
                }
            }else if (clickBu == 12) {
                if (FCUIConfig.getIn().checkUIClick("singlePlayNewBack", x, y)) {
                    FCRMaster.fcv.setEnterBu(14);
                } else if (FCUIConfig.getIn().checkUIClick("singlePlayNewOk", x, y)) {
                    FCRMaster.fcv.setEnterBu(15);
                }
            }
        }
    }

    @Override
    public void listen_mouse_click_game(MouseEvent g) {
        int ti = FCRMaster.fcv.getFrames();
        int x = g.getX(), y = g.getY();
        if (ti > 960) {// 单人游戏准备界面
            if (FCRMaster.fcv.getClickBu() == 1) {
                if (FCUIConfig.getIn().checkUIClick("singlePlayBack", x, y)) {
                    FCRMaster.fcv.setInterfacenum(0);
                    FCRMaster.fcv.setClickBu(0);
                    FCRMaster.sound.setBackgroundMusic(5);
                } else if (FCUIConfig.getIn().checkUIClick("singlePlayNew", x, y)) {
                    FCRMaster.fcv.setClickBu(12);
                    FCRMaster.playerName.setVisible(true);
                } else if (FCUIConfig.getIn().checkUIClick("singlePlayStart", x, y)) {
                    if (FCRMaster.saveList.getSaveNum() > 0 && FCRMaster.fcv.getSaveMouseClick() > -1) {
                        if (FCRMaster.fcv.getSaveMouseClick() >= FCRMaster.saveList.getSaveNum())
                            FCRMaster.fcv.setSaveMouseClick(0);
                        FCRMaster.fcv.setInterfacenum(4);
                        FCRMaster.fcv.setSaveUploadIsOk(0);
                        if(asServer){
                            fcv.setSever(true);
                        }else {
                            fcv.setSever(false);
                        }
                        new Thread(new FCRLoadData()).start();
                    }
                } else {
                    if (FCUIConfig.getIn().checkUIClick("singlePlayArea", x, y)) {
                        int witchFile = (int) ((y - 224 * FCRMaster.fcv.getProportion()) / (43* FCRMaster.fcv.getProportion()));
                        System.out.println("点了第 "+witchFile+" 个存档");
                        //计算点了第几个存档的
                        FCRMaster.fcv.setSaveMouseClick(witchFile);
                        if (FCRMaster.fcv.getSaveMouseClick() >= FCRMaster.saveList.getSaveTimelist().size()) {
                            FCRMaster.fcv.setSaveMouseClick(-1);
                        }
                    } else {
                        FCRMaster.fcv.setSaveMouseClick(-1);
                    }
                }
            }
            if (FCRMaster.fcv.getClickBu() == 12) {
                if (FCUIConfig.getIn().checkUIClick("singlePlayNewBack", x, y)) {
                    FCRMaster.fcv.setClickBu(1);
                    FCRMaster.playerName.setVisible(false);
                } else if (FCUIConfig.getIn().checkUIClick("singlePlayNewOk", x, y)) {
                    if (FCRMaster.playerName.getText().length() > 2) {
                        FCRMaster.fcv.setClickBu(1);
                        FCRMaster.fcv.setInterfacenum(4);
                        FCRMaster.fcv.setSaveMouseClick(-1);
                        FCRMaster.fcv.setSaveUploadIsOk(1);
                        FCRMaster.player1.setSelectPlayer(FCRMaster.fcv.getSelectPlayer());
                        FCRMaster.player1.setName(FCRMaster.playerName.getText());
                        FCRMaster.playerName.setVisible(false);
                        FCRMaster.gameSave.setTime(DateUtil.getNowTime());
                        if(asServer){
                            fcv.setSever(true);
                        }else {
                            fcv.setSever(false);
                        }
                        new Thread(new FCRLoadData()).start();
                    }
                } else if (FCUIConfig.getIn().checkUIClick("singlePlayNew1", x, y)) {
                    FCRMaster.fcv.setSelectPlayer(0);
                } else if (FCUIConfig.getIn().checkUIClick("singlePlayNew2", x, y)) {
                    FCRMaster.fcv.setSelectPlayer(1);
                } else if (FCUIConfig.getIn().checkUIClick("singlePlayNew3", x, y)) {
                    FCRMaster.fcv.setSelectPlayer(2);
                }
            }
        }
    }
}
